package components.contorls.superClass
{
    import components.core.UIComponent;
    import components.managers.PopUpManager;
    import components.managers.SystemManager;
    import components.utils.EventListenerUtil;
    
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.utils.getDefinitionByName;
    
    import mx.events.FlexEvent;

    [Event(name="selectionChange", type="mx.events.FlexEvent")]
    public class BaseDropDownList extends UIComponent
    {
        protected var group:BaseGroup;

        private var _dataProvider:Array = [];

        private var _itemRendererClass:Class = null;

        private var _selectedItem:Object = null;

        private var _selectedIndex:int = -1;

        /**
         *弹出list的本地坐标
         */
        private var _listLocalPoint:Point;

        /**
         *是否弹出list
         */
        private var _isOpen:Boolean = false;

        private var _dataProviderChange:Boolean;

        private var _itemRendererClassChange:Boolean;

        public function BaseDropDownList()
        {
            super();

            if (labelTF)
            {
                labelTF.mouseEnabled = false;
                labelTF.mouseChildren = false;
            }

            _listLocalPoint = new Point(group.x, group.y);

			EventListenerUtil.addEventListener(button,MouseEvent.CLICK, button_mouseClickHandler);
            removeChild(group);
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get labelTF():BaseLabel
        {
            return getChildByName("labelSkin") as BaseLabel;
        }

        public function get button():BaseButton
        {
            return getChildByName("buttonSkin") as BaseButton;
        }

        public function get boxBackgroundDisplay():DisplayObject
        {
            return getChildByName("boxBackgroundSkin") as DisplayObject;
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();

            if (!_initialized && !group)
            {
                group = getChildByName("groupSkin") as BaseGroup;
            }

            if (_dataProviderChange || _itemRendererClassChange)
            {
                if (_selectedIndex + 1 > dataProvider.length)
                    selectedIndex = -1;

                createList();

                _itemRendererClassChange = false;
                _dataProviderChange = false;
            }

            if (labelTF)
            {
                if (_selectedItem != null)
                    labelTF.text = String(_selectedItem);
                else
                    labelTF.text = "";
            }
        }

        protected override function measureSizes():Boolean
        {
            if (super.measureSizes())
            {
                button.x = width - button.width;
                boxBackgroundDisplay.width = width;
                if (labelTF)
                    labelTF.width = width - 4 - 22.05;

                group.width = width;

                return true;
            }

            return false;
        }

        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/

        [Inspectable(type="Array")]
        public function get dataProvider():Array
        {
            return _dataProvider;
        }

        public function set dataProvider(value:Array):void
        {
            _dataProvider = value;

            _dataProviderChange = true;

            invalidate();
        }

        [Inspectable(defaultValue="-1")]
        public function get selectedIndex():int
        {
            return _selectedIndex;
        }

        public function set selectedIndex(value:int):void
        {
            _selectedIndex = value;

            if (_selectedIndex == -1)
                cleanSelected();
            else
                selectedItem = dataProvider[_selectedIndex];
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        public function get itemRendererClass():Class
        {
            return _itemRendererClass;
        }

        public function set itemRendererClass(value:Class):void
        {
            _itemRendererClass = value;

            _itemRendererClassChange = true;
            invalidate();
        }

        private function createList():void
        {
			var item:BaseItemRenderer;
            while (group.numChildren > 0)
			{
				item=group.removeChildAt(0) as BaseItemRenderer;
				if(item)
					item.destroy();
			}

            if (itemRendererClass == null)
                _itemRendererClass = Class(getDefinitionByName("components.contorls.ItemRenderer"));

            for (var i:int = 0; i < dataProvider.length; i++)
            {
                item = new itemRendererClass();
                item.selectedPossible = false;
				EventListenerUtil.addEventListener(item,Event.SELECT, selectedHandler);
                item.focusComp = focusComp;
                item.data = dataProvider[i];
                group.addChild(item);
            }
        }

        public function get selectedItem():Object
        {
            return _selectedItem;
        }

        public function set selectedItem(value:Object):void
        {
            if (_selectedItem == value)
                return;

            _selectedItem = value;

            if (_selectedItem == null)
                cleanSelected();
            else
            {
                var index:int = -1;
                for (var i:int = 0; i < dataProvider.length; i++)
                {
                    if (dataProvider[i] == _selectedItem)
                    {
                        index = i;
                        break;
                    }
                }

                if (index == -1)
                {
                    _selectedIndex = -1;
                    _selectedItem = null;
                }
                else
                    _selectedIndex = index;
            }

            dispatchEvent(new FlexEvent(FlexEvent.SELECTION_CHANGE));

            invalidate();
        }

        protected function cleanSelected():void
        {
            _selectedIndex = -1;
            _selectedItem = null;
        }

        protected function selectedHandler(event:Event):void
        {
            var item:BaseItemRenderer = event.currentTarget as BaseItemRenderer;

            selectedItem = item.data;
            isOpen = false;
        }

        protected function button_mouseClickHandler(event:MouseEvent):void
        {
            isOpen = !isOpen;
        }

        protected override function rollOverHandler(event:MouseEvent):void
        {
            super.rollOverHandler(event);
            button.isOver = true;
            button.setOverState();
        }


        protected override function rollOutHandler(event:MouseEvent):void
        {
            super.rollOutHandler(event);

            button.isOver = false;
            button.setOutState();
        }

        protected override function mouseDownHandler(event:MouseEvent):void
        {
            super.mouseDownHandler(event);

            button.setDownState();
        }

        protected override function mouseUpHandler(event:MouseEvent):void
        {
            super.mouseUpHandler(event);
            button.setUpState();
        }

        public function get isOpen():Boolean
        {
            return _isOpen;
        }

        public function set isOpen(value:Boolean):void
        {
            _isOpen = value;

            if (_isOpen)
                showList();
            else
                hideList();
        }

        protected function hideList():void
        {
            PopUpManager.removePopUp(group);
            _isOpen = false;
        }

        protected function showList():void
        {
            setVListXY();

            PopUpManager.addPopUp(group, false);
        }

        /**
         *设置List坐标，把本地坐标转换为全局坐标
         *
         */
        protected function setVListXY():void
        {
            var popPoint:Point = localToGlobal(_listLocalPoint);
            var app:SystemManager = SystemManager.instance;

            if (popPoint.y + group.height > app.height)
                popPoint.y = y - group.height - 2;

            group.x = popPoint.x;
            group.y = popPoint.y;
        }

        public override function set focusComp(value:UIComponent):void
        {
            super.focusComp = value;

            if (!group)
                group = getChildByName("groupSkin") as BaseGroup;

            group.focusComp = focusComp;

            if (labelTF)
                labelTF.focusComp = focusComp;
            button.focusComp = focusComp;
        }

        public override function focusOut():void
        {
            super.focusOut();
            isOpen = false;
        }

        public override function destroy():void
        {
            super.destroy();
            group = null;
            _dataProvider = null;
            _listLocalPoint = null;
        }

        public override function validateNow():void
        {
            super.validateNow();
            if (labelTF)
                labelTF.validateNow();
        }

        protected override function frameConstructedHandler(event:Event):void
        {
            super.frameConstructedHandler(event);
            if (labelTF)
                labelTF.mouseChildren = labelTF.mouseEnabled = false;

        }
    }
}